package com.zhuss.core.processor;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.config.type.DeleteConfigTypeBo;
import com.zhuss.bean.vo.core.config.type.DetailConfigTypeVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.ConfigTypeDao;
import com.zhuss.core.db.entity.ConfigType;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.AabException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.util.JsonUtil;

/**
 * 删除配置类型 processor
 * @Author: zhushanshuo
 * @Date: 2020/02/20 21:55
 */
@Component("configTypeDelete")
public class ConfigTypeDelete extends AbstractProcessor<Object, DeleteConfigTypeBo> implements Processor<Object, DeleteConfigTypeBo> {

    @Autowired
    private ConfigTypeDao configTypeDao;
    
    @Autowired
	@Qualifier("configTypeRead")
	private ConfigTypeRead configTypeRead;
    
    @Autowired
	@Qualifier("configRead")
	private ConfigRead configRead;
    
    /**
     * 校验
     */
	@Override
	@SuppressWarnings("unchecked")
    public void check(DeleteConfigTypeBo bo) throws AabException {
    	Long configTypeId = bo.getConfigTypeId();
    	Integer area = bo.getConcurrentArea();
    	DetailConfigTypeVo entity = (DetailConfigTypeVo) configTypeRead.detail(configTypeId);
    	String type = entity.getType();
    	List<Long> configIdList = (List<Long>) configRead.listConfigId(type, area);
    	if (!configIdList.isEmpty()) {
    		throw new AabException(ExceptionCode.不能删除包含配置的配置类型);
    	}
    }
    
    /**
     * 操作方法
     */
    @Override
    public Object process(DeleteConfigTypeBo param) throws AabException {
    	Long configTypeId = param.getConfigTypeId();
    	String value = ((DetailConfigTypeVo) configTypeRead.detail(configTypeId)).getValue();
    	
    	ConfigType record = new ConfigType();
    	record.setConfigTypeId(configTypeId);
    	record.setDeleted(param.getDeleted());
    	
    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	configTypeDao.delete(record);
    	param.setName(value);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(DeleteConfigTypeBo param) throws AabException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.配置类型删除.key());
    	record.setTargetId(param.getConfigTypeId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
